package com.vincent.seeyou.ui;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.vincent.seeyou.R;
import com.vincent.seeyou.model.BmobConstants;
import com.vincent.seeyou.util.CommonUtils;

import cn.bmob.im.util.BmobLog;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by vincent on 14-9-1.
 */
public class LoginActivity extends BaseActivity{

    private EditText editText_name,editText_password;
    private RegisterBroadcastReceiver receiver = new RegisterBroadcastReceiver();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //注册退出广播
        IntentFilter filter = new IntentFilter();
        filter.addAction(BmobConstants.ACTION_REGISTER_SUCCESS_FINISH);
        registerReceiver(receiver, filter);

        editText_name = (EditText)findViewById(R.id.editText_name);
        editText_password = (EditText)findViewById(R.id.editText_password);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterReceiver(receiver);
    }


    public void onLogin(View view) {
        boolean isNetConnected = CommonUtils.isNetworkAvailable(this);

        if(!isNetConnected){

            ShowToast(R.string.tip_network);

            return;
        }
        login();
    }

    public void onRegister(View view) {
        Intent intent = new Intent(LoginActivity.this,
                RegisterActivity.class);
        startActivity(intent);
    }


    private void login(){
        String name = editText_name.getText().toString();
        String password = editText_password.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ShowToast(R.string.tip_error_username);
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ShowToast(R.string.tip_error_password);
            return;
        }

        final ProgressDialog progress = new ProgressDialog(
                LoginActivity.this);
        progress.setMessage(getString(R.string.tip_is_logining));
        progress.setCanceledOnTouchOutside(false);
        progress.show();
        bmobUserManager.login(name, password, new SaveListener() {

            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        progress.setMessage(getString(R.string.tip_is_loadling_friends_list));
                    }
                });
                //更新用户的地理位置以及好友的资料
                updateUserInfos();
                progress.dismiss();
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(int errorcode, String arg0) {
                // TODO Auto-generated method stub
                progress.dismiss();
                BmobLog.i(arg0);
                ShowToast(arg0);
            }
        });

    }



    public class RegisterBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && BmobConstants.ACTION_REGISTER_SUCCESS_FINISH.equals(intent.getAction())) {
                finish();
            }
        }

    }
}
